package com.example.fang.demodruid.controller;


import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.fang.demodruid.annotation.CountTimesAnnotation;
import com.example.fang.demodruid.entity.TOrderInfo;
import com.example.fang.demodruid.mapper.TOrderInfoMapper;
import com.example.fang.demodruid.service.ITOrderInfoService;
import com.example.fang.demodruid.util.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;
import java.util.*;

/**
 * log4j.properties和logback.xml分别对应log4j与slf4j
 */
@RestController
@RequestMapping("/demodruid/")
@Slf4j
public class TOrderInfoController {

    @Autowired
    private ITOrderInfoService itOrderInfoService;

    @Autowired
    private DataSource dataSource;

    @Autowired
    private TOrderInfoMapper tOrderInfoMapper;

    @RequestMapping("/sql")
    public Map<String,Object> test(Integer id){
        log.info("sql查询开始");
        Map<String,Object> map=new HashMap<>();
        QueryWrapper<TOrderInfo> queryWrapper=new QueryWrapper<>();
        if(id!=null){
            queryWrapper.lambda().eq(TOrderInfo::getId,id);
        }
        List<TOrderInfo> list=itOrderInfoService.list(queryWrapper);
        map.put("data",list);
        log.info("sql查询结束");
        return  map;
    };

    /**
     * 单个更新
     * @return
     */
    @RequestMapping("/test1")
    public String test1(){
        TOrderInfo tOrderInfo=new TOrderInfo();
        tOrderInfo.setTitle("测试订单标题");
        UpdateWrapper<TOrderInfo> updateWrapper=new UpdateWrapper();
        updateWrapper.lambda().eq(TOrderInfo::getId,1L);
        itOrderInfoService.update(tOrderInfo,updateWrapper);
        return  "success";
    }

    /**
     * 批量更新
     * @return
     */
    @RequestMapping("/test11")
    public String test11(){
        TOrderInfo tOrderInfo=new TOrderInfo();
        List<TOrderInfo> list=new ArrayList<>();
        List<TOrderInfo> listobjs=itOrderInfoService.list();
        for(int i=0;i<listobjs.size();i++){
            listobjs.get(i).setTitle("测试订单标题ccb"+i);
            list.add(listobjs.get(i));
        }
        itOrderInfoService.updateBatchById(list);
        return  "success";
    }

    /**
     * 批量插入
     * @return
     */
    @RequestMapping("/test2")
    @CountTimesAnnotation
    public String test2(){
        List<TOrderInfo> tOrderInfos=new ArrayList<>();
        for(int i=0;i<100000;i++){
            TOrderInfo tOrderInfo=new TOrderInfo();
            tOrderInfo.setTitle("测试订单标题fang"+i);
            tOrderInfo.setCreateTime(new Date());
            tOrderInfo.setUpdateTime(new Date());
            tOrderInfo.setCodeUrl("11");
            tOrderInfo.setOrderNo("11");
            tOrderInfo.setOrderStatus("11");
            tOrderInfo.setProductId(11L);
            tOrderInfo.setTotalFee(11);
            tOrderInfo.setUserId(1L);
            tOrderInfos.add(tOrderInfo);
        }
        itOrderInfoService.saveBatch(tOrderInfos,10000);
        return  "success";
    }

    /**
     * 循环单个新增
     * @return
     */
    @RequestMapping("/test3")
    public String test3(){
        List<TOrderInfo> tOrderInfos=new ArrayList<>();

        for(int i=0;i<10;i++){
            TOrderInfo tOrderInfo=new TOrderInfo();
            tOrderInfo.setTitle("测试订单标题chen"+i);
            tOrderInfo.setCreateTime(new Date());
            tOrderInfo.setUpdateTime(new Date());
            tOrderInfo.setCodeUrl("11");
            tOrderInfo.setOrderNo("11");
            tOrderInfo.setOrderStatus("11");
            if(i==5){
                tOrderInfo.setTitle("测试订单标题0");
            }
            tOrderInfo.setProductId(11L);
            tOrderInfo.setTotalFee(11);
            tOrderInfo.setUserId(1L);
            tOrderInfos.add(tOrderInfo);
            itOrderInfoService.save(tOrderInfo);
        }
        return  "success";
    }

    /**
     * 获取连接池当前连接数
     * @return
     */
    @RequestMapping("/test4")
    public String test4(){
        DruidDataSource drs = (DruidDataSource)SpringUtils.getBean(DataSource.class);;//前面实现各有不同主要从配置bean中获取DruidDataSource
        int activeCount = drs.getActiveCount();//当前连接数
        System.out.println("DruidDataSource activeCount:"+activeCount);
        System.out.println("DruidDataSource MaxActive:"+drs.getMaxActive());
        System.out.println("DruidDataSource InitialSize:"+drs.getInitialSize());
        return activeCount+"";
    };

    /**
     * 测试druid防火墙
     * @return
     */
    @RequestMapping("/test5")
    public String test5(){
        tOrderInfoMapper.delete(null);
        //return activeCount+"";
        return "success";
    }


    /**
     * 测试druid防火墙
     * @return
     */
    @RequestMapping("/test6")
    public String test6() throws InterruptedException {
        log.info("并发测试请求进来");
        //Thread.sleep(500);
        return "success";
    }
}
